package contact.servlets;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import contact.domain.Address;
import contact.domain.Contact;
import contact.domain.DAOContact;
import contact.domain.DAOPhoneNumber;
import contact.domain.PhoneNumber;


public class NewContact extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private DAOContact daoContact;
    
    public NewContact() {
    }
    

	public DAOContact getDaoContact() {
		return daoContact;
	}


	public void setDaoContact(DAOContact daoContact) {
		this.daoContact = daoContact;
	}


	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		DAOPhoneNumber daoPhone = new DAOPhoneNumber();
		daoContact = new DAOContact();
		Contact contact = new Contact(
				request.getParameter("firstname"),
				request.getParameter("lastname"),
				request.getParameter("email"));
		Address address = new Address(
				request.getParameter("street"),
				request.getParameter("city"),
				request.getParameter("zip"),
				request.getParameter("country"));
		contact.setAddress(address);
		String mobile = request.getParameter("mobile");
		String home = request.getParameter("home");
		String job = request.getParameter("job");
		
		daoContact.create(contact);
		
		PhoneNumber phone = new PhoneNumber();
		phone.setContact(contact);
		phone.setPhoneKind(PhoneNumber.MOBILE);
		phone.setPhoneNumber(mobile);
		contact.getProfiles().add(phone);
		daoPhone.create(phone);
		phone = new PhoneNumber();
		phone.setContact(contact);
		phone.setPhoneKind(PhoneNumber.HOME);
		phone.setPhoneNumber(home);
		contact.getProfiles().add(phone);
		daoPhone.create(phone);
		phone = new PhoneNumber();
		phone.setContact(contact);
		phone.setPhoneKind(PhoneNumber.JOB);
		phone.setPhoneNumber(job);
		contact.getProfiles().add(phone);
		daoPhone.create(phone);
		
		System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$****************************$$$$$$$$$$$$$$$$$$$$$$$$");
		
		RequestDispatcher rd = request.getRequestDispatcher("/accueil.jsp");
		rd.forward(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	}

}
